using System.Collections.Generic;
using UnityEngine;

public class MyTeamLayer : MonoBehaviour
{
	public enum ShownState
	{
		NeedToLogin = 0,
		ShowingFriends = 1
	}

	protected struct PlayerProgress
	{
		public string pictureUrl;

		public string playerName;

		public int starsCollected;
	}

	public UITable table;

	public GameObject teamMateTagPrefab;

	public Transform loginToFacebookButton;

	public UILabel loginMessage;

	public UIScrollView scroll;

	public UILabel title;

	public UISprite flag;

	public UILabel headerLabel;

	public UIWidget topWidget;

	protected List<TeamMateTag> teamMateTags = new List<TeamMateTag>();

	private ShownState state;

	private void OnEnable()
	{
		title.text = "My Team";
		GameConstants.SetFlag(flag, GGPlayerSettings.instance.Model.flag);
		if (!GGPlayerSettings.instance.Model.hasLoggedIntoFacebook)
		{
			ShowLoginToFacebook();
		}
		else
		{
			ShowMyTeam();
		}
		BehaviourSingletonInit<FriendProfilesManager>.instance.onProfilesAvailable -= OnProfilesAvailable;
		BehaviourSingletonInit<FriendProfilesManager>.instance.onProfilesAvailable += OnProfilesAvailable;
		BehaviourSingleton<GGNotificationCenter>.instance.onMessage -= OnMessageDelegate;
		BehaviourSingleton<GGNotificationCenter>.instance.onMessage += OnMessageDelegate;
	}

	private void OnMessageDelegate(string message)
	{
		if ("MessageConflictResolved".Equals(message) && GGPlayerSettings.instance.Model.hasLoggedIntoFacebook)
		{
			ShowMyTeam();
		}
	}

	private void OnDisable()
	{
		BehaviourSingletonInit<FriendProfilesManager>.instance.onProfilesAvailable -= OnProfilesAvailable;
		BehaviourSingleton<GGNotificationCenter>.instance.onMessage -= OnMessageDelegate;
	}

	private void OnProfilesAvailable()
	{
		List<FriendProfileConfig.FriendProfileWrapper> allProfiles = BehaviourSingletonInit<FriendProfilesManager>.instance.allProfiles;
		FriendProfileConfig.FriendProfileWrapper myProfile = BehaviourSingletonInit<FriendProfilesManager>.instance.myProfile;
		headerLabel.text = "Team Members: " + Mathf.Max(1, allProfiles.Count);
		int num = 0;
		if (allProfiles == null)
		{
			return;
		}
		List<PlayerProgress> list = new List<PlayerProgress>();
		foreach (FriendProfileConfig.FriendProfileWrapper item in allProfiles)
		{
			list.Add(new PlayerProgress
			{
				pictureUrl = item.pictureUrl,
				playerName = item.playerName,
				starsCollected = item.starsCollected
			});
		}
		list.Sort((PlayerProgress p1, PlayerProgress p2) => p2.starsCollected.CompareTo(p1.starsCollected));
		foreach (PlayerProgress item2 in list)
		{
			if (num >= teamMateTags.Count)
			{
				teamMateTags.Add(NGUITools.AddChild(table.gameObject, teamMateTagPrefab).GetComponent<TeamMateTag>());
			}
			TeamMateTag teamMateTag = teamMateTags[num];
			teamMateTag.gameObject.SetActive(true);
			teamMateTag.gameObject.name = GGFormat.IndexName(num);
			teamMateTag.Init(num, item2.playerName, item2.pictureUrl, item2.starsCollected);
			num++;
		}
		table.Reposition();
		StopAllCoroutines();
		StartCoroutine(UITools.DoAlignToTopOnScroll(topWidget, scroll, 0f));
	}

	private void ShowMyTeam()
	{
		state = ShownState.ShowingFriends;
		SetLoginActive(false);
		OnProfilesAvailable();
	}

	private void ShowLoginToFacebook()
	{
		state = ShownState.NeedToLogin;
		SetLoginActive(true);
	}

	private void SetLoginActive(bool active)
	{
		loginToFacebookButton.gameObject.SetActive(active);
		loginMessage.cachedGameObject.SetActive(active);
		scroll.gameObject.SetActive(!active);
		if (active)
		{
			title.text = "My Team (Requires Facebook)";
		}
		else
		{
			title.text = "My Team";
		}
	}

	private void Update()
	{
		if (state == ShownState.NeedToLogin && GGPlayerSettings.instance.Model.hasLoggedIntoFacebook)
		{
			ShowMyTeam();
		}
	}
}
